package LinkedList;

public class p707设计链表 {
    class MyLinkedList {
        Node head;
        int size;

        class Node{
            int val;
            Node next;
            Node(){}
            Node(int val){
                this.val = val;
            }
        }

        public MyLinkedList() {
            head = new Node(0);
            size = 0;
        }

        public int get(int index) {
            if (index < 0 || index >= size){
                return -1;
            }
            Node p = head;
            for (int i = 0; i <= index; i++) {
                p = p.next;
            }
            return p.val;
        }

        public void addAtHead(int val) {
            addAtIndex(0, val);
        }

        public void addAtTail(int val) {
            addAtIndex(size, val);
        }

        public void addAtIndex(int index, int val) {
            if (index > size){
                return;
            }
            Node node = new Node(val);
            Node p = head;
            for (int i = 0; i < index; i++) {
                p = p.next;
            }
            node.next = p.next;
            p.next = node;
            size++;
        }

        public void deleteAtIndex(int index) {
            if (index < 0 || index >= size) {
                return;
            }
            Node p = head;
            for (int i = 0; i < index; i++) {
                p = p.next;
            }
            p.next = p.next.next;
            size--;
        }
    }
}
